/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

/**
 *
 * @author aeguzkiza
 */
import GUI.JFrameBase;
import java.text.ParseException;
import java.util.Date;
import javax.swing.JOptionPane;
import model.DB2Hash;

public class PatientPanelView {
    // Text by default
    private String fieldNames = "\n- Nº HCE \n- Nombre\n- Apellido1\n- Apellido2\n- Fecha de nacimiento\n- Sexo";
    private String fieldNames2 = "\n- Nombre\n- Apellido1\n- Apellido2";
    public String patientExists = "Ya existe un paciente con este identificador";
    private String newPatient = "Identificador nuevo en la base de datos";

    // Initialize variables
    public boolean isNew;
    private final JFrameBase frame;

    public PatientPanelView(JFrameBase frame) {
        this.frame = frame;
    }

    public void updatePatientData(DB2Hash p) throws ParseException {
        this.isNew = false;
        frame.HCEInput.setText(p.get("PatientID"));
        frame.NameInput.setText(p.get("PatientsName"));
        frame.SurnameInput.setText(p.get("PatientsSurName"));
        String PatientsBirthDate = frame.parseDate(p.get("PatientsBirthDate"));
        //frame.BirthDateInput.setText(PatientsBirthDate);
        Date date = frame.dateFormat.parse(PatientsBirthDate);
        frame.BirthDateInput.setDate(date);
        
        if(p.get("PatientsSex").toUpperCase().equals("M")){
            frame.SexM_Radio.setSelected(true);
        }else if(p.get("PatientsSex").toUpperCase().equals("F")){
            frame.SexF_Radio.setSelected(true);
        }else{
            frame.SexButtonGroup.clearSelection();
        }
        
        
        frame.PatientCommentsTextArea.setText(p.get("PatientComments"));
    }

    public DB2Hash getPatientData() {
        DB2Hash patient = new DB2Hash();
        //ArrayList patient = new ArrayList();
        System.out.println("Get PatientData");
        // Read data and check for errors:
        String HCE = frame.HCEInput.getText();
        String Name = frame.NameInput.getText();
        if(!Name.isEmpty()){Name = Name.substring(0,1).toUpperCase() + Name.substring(1);}   // First letter to capital letters.
        String SurName = frame.SurnameInput.getText();
        if(!SurName.isEmpty()){SurName = SurName.substring(0,1).toUpperCase() + SurName.substring(1);}   // First letter to capital letters.
        //String BirthDate = frame.BirthDateInput.getText();
        Date birthDate = frame.BirthDateInput.getDate();
        String BirthDate = "";        
        if(birthDate != null){  // If birthDate is NULL, formatter will show an error.
                BirthDate = frame.dateFormat.format(birthDate);  
        }   
        System.out.println("Nombre: "+Name);
        System.out.println("Fecha nacimiento: "+birthDate);
        System.out.println("Fecha nacimiento: "+BirthDate);
        String Sex = "";
        if(frame.SexM_Radio.isSelected()){Sex= "M";
        }else if(frame.SexF_Radio.isSelected()){Sex= "F";}
        String PatientComments = frame.PatientCommentsTextArea.getText();

        // Check for errors:        
        if(HCE.isEmpty() || Name.isEmpty() || SurName.isEmpty() || BirthDate.isEmpty() || Sex.isEmpty()){
            JOptionPane.showMessageDialog(frame.DemographicPanel, this.frame.emptyField+fieldNames, frame.errorTxt,JOptionPane.ERROR_MESSAGE);
        }else if(Name.charAt(0) == ' ' || SurName.charAt(0) == ' ' ){
            JOptionPane.showMessageDialog(frame.DemographicPanel, this.frame.emptyChar+fieldNames2, frame.errorTxt,JOptionPane.ERROR_MESSAGE);
        }else{
            /*System.out.println("HCE: "+HCE);
            System.out.println("Name: "+Name);
            System.out.println("SurName: "+SurName);
            System.out.println("BirthDate: "+BirthDate);
            System.out.println("Sex: "+Sex);
            System.out.println("PatientComments: "+PatientComments);*/

            // Save data:
            //patient.set("Id", null);
            if(frame.HCE_checkLabel.getText().equals(newPatient)){
                this.isNew = true;
            }else if(frame.HCE_checkLabel.getText().equals(patientExists)){
                this.isNew = false;
            }
            //System.out.println("isNew: "+this.isNew);
            
            patient.put("PatientID", HCE);
            patient.put("PatientsName", Name);
            patient.put("PatientsSurName", SurName);
            patient.put("PatientsSex", Sex);
            patient.put("PatientsBirthDate", BirthDate);
            patient.put("PatientComments", PatientComments);
            
            /*// FAST DATA INPUT:
            patient.set("Id", null);
            patient.set("PatientID", "1");
            patient.set("PatientsName", "Moe");
            patient.set("PatientsSurName", "Sislack Springfield");
            patient.set("PatientsSex", "M");
            patient.set("PatientsBirthDate", "1976/03/10");
            patient.set("PatientComments", "Hola mundo");*/
            
            
        }
        
        
        return patient;
    }
    public void checkHCE(int HCE_exists) {
        if(HCE_exists == 0){
            frame.HCE_checkLabel.setText(this.patientExists);
            this.isNew = false;
        }else if(HCE_exists == 1){
            // Show info message.
            String HCE = frame.HCEInput.getText();  // Save HCE value before of clear it.
            clearAll();
            frame.HCEInput.setText(HCE);    // Writes HCE value.
            frame.HCE_checkLabel.setText(this.newPatient);
            this.isNew = true;
            
        }else{
            //this.isNew = true;    // Has no sense, cause with empty HCE field we can't make changes over Patient data.
            frame.HCE_checkLabel.setText(null);
        }
    }
    public void clearAll(){
        // Clear all data.
        frame.HCE_checkLabel.setText(null);
        frame.HCEInput.setText(null);
        frame.NameInput.setText(null);
        frame.SurnameInput.setText(null);
        //frame.BirthDateInput.setText(null);
        frame.BirthDateInput.setDate(null);
        frame.SexButtonGroup.clearSelection();
        frame.PatientCommentsTextArea.setText(null);
    }

}
